package csc.fresher.view;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.DataModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.entity.SystemUser;
import csc.fresher.service.SystemUserChangeStateService;
import csc.fresher.service.SystemUserService;

@Component
@Scope("session")
public class SystemUserChangeState implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private SystemUserChangeStateService systemUserChangeStateService;

	@Autowired
	private SystemLoginManageBean systemLoginManageBean;

	@Autowired
	private SystemUserService systemUserService;

	private String selectedUserState;
	private List<SystemUser> systemUserList;
	private SystemUserDataModel systemUserDataModel;
	private SystemUser systemUser;
	private Boolean selectAll;
	private Map<Long, Boolean> selectedCheckbox = new HashMap<Long, Boolean>();
	private String changeButtonValue;
	private String selectSearchType;
	private String searchValue;

	private List<SystemUser> selectedSystemUsers;


	public List<SystemUser> getSelectedSystemUsers() {
		return selectedSystemUsers;
	}

	public void setSelectedSystemUsers(List<SystemUser> selectedSystemUsers) {
		this.selectedSystemUsers = selectedSystemUsers;
	}

	public SystemUserChangeStateService getSystemUserApproveService() {
		return systemUserChangeStateService;
	}

	public void setSystemUserApproveService(
			SystemUserChangeStateService systemUserChangeStateService) {
		this.systemUserChangeStateService = systemUserChangeStateService;
	}

	public SystemLoginManageBean getSystemLoginManageBean() {
		return systemLoginManageBean;
	}

	public void setSystemLoginManageBean(
			SystemLoginManageBean systemLoginManageBean) {
		this.systemLoginManageBean = systemLoginManageBean;
	}

	public SystemUserService getSystemUserService() {
		return systemUserService;
	}

	public void setSystemUserService(SystemUserService systemUserService) {
		this.systemUserService = systemUserService;
	}

	public String getSelectedUserState() {
		return selectedUserState;
	}

	public void setSelectedUserState(String selectedUserState) {
		this.selectedUserState = selectedUserState;
	}

	public List<SystemUser> getSystemUserList() {
		return systemUserList;
	}

	public void setSystemUserList(List<SystemUser> systemUserList) {
		this.systemUserList = systemUserList;
	}


	public DataModel<SystemUser> getSystemUserDataModel() {
		return systemUserDataModel;
	}

	public void setSystemUserListDataModel(
			SystemUserDataModel systemUserDataModel) {
		this.systemUserDataModel = systemUserDataModel;
	}

	public SystemUser getSystemUser() {
		return systemUser;
	}

	public void setSystemUser(SystemUser systemUser) {
		this.systemUser = systemUser;
	}

	public Boolean getSelectAll() {
		return selectAll;
	}

	public void setSelectAll(Boolean selectAll) {
		this.selectAll = selectAll;
	}

	public Map<Long, Boolean> getSelectedCheckbox() {
		return selectedCheckbox;
	}

	public void setSelectedCheckbox(Map<Long, Boolean> selectedCheckbox) {
		this.selectedCheckbox = selectedCheckbox;
	}

	public String getChangeButtonValue() {
		return changeButtonValue;
	}

	public void setChangeButtonValue(String changeButtonValue) {
		this.changeButtonValue = changeButtonValue;
	}

	public SystemUserChangeStateService getSystemUserChangeStateService() {
		return systemUserChangeStateService;
	}

	public void setSystemUserChangeStateService(
			SystemUserChangeStateService systemUserChangeStateService) {
		this.systemUserChangeStateService = systemUserChangeStateService;
	}

	public String getSelectSearchType() {
		return selectSearchType;
	}

	public void setSelectSearchType(String selectSearchType) {
		this.selectSearchType = selectSearchType;
	}

	public String getSearchValue() {
		return searchValue;
	}

	public void setSearchValue(String searchValue) {
		this.searchValue = searchValue;
	}


	@PostConstruct
	public void init() {
		try {
			this.systemUser = null;
			this.systemUserList = firstLoad();
			this.systemUserDataModel = new SystemUserDataModel(systemUserList);
			this.changeButtonValue = "Active";
			//this.resultCheckAll = "false";
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// first load
	public List<SystemUser> firstLoad() {
		systemUserList = systemUserService.getSystemUserByState("active");
		return systemUserList;
	}

	// change state button
	public String changeToDisable() {
		boolean flagLoginUserIsAdmin = false;
		boolean flagModifiedIsAdmin = false;
		
		SystemUser system = new SystemUser();
		system = systemLoginManageBean.getSystemUser();
		List<Long> listroleLogin = systemUserService.getRoleByUserName(system.getUserName());
		for (int i = 0; i < listroleLogin.size(); i++) {
			if (1 == listroleLogin.get(i)) {
				flagLoginUserIsAdmin = true;
			}
		}
		
		for (SystemUser s : selectedSystemUsers) {
			List<Long> listrole = systemUserService.getRoleByUserName(s.getUserName());
			for (int i = 0; i < listrole.size(); i++) {
				if (1 == listrole.get(i)) {
					flagModifiedIsAdmin = true;
				}
			}
			
			if (flagLoginUserIsAdmin == false && flagModifiedIsAdmin == true) {
				FacesContext context = FacesContext.getCurrentInstance();
				context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "You can not disable admin!", null));	
			}
			else{
				SystemUser systemUser = new SystemUser();
				systemUser.setSystemUserID(s.getSystemUserID());
				systemUserChangeStateService.changeSystemUserStateToDisable(systemUser);
			}
			flagModifiedIsAdmin = false;
		}
		systemUserList = systemUserService.getSystemUserByState("active");
		this.systemUserDataModel = new SystemUserDataModel(systemUserList);
		
		return "ChangeStateUser";
	}	
	public String changeToActive() {
		//Map<Long, Boolean> selectedCheckbox = getSelectedCheckbox();
		for (SystemUser s : selectedSystemUsers) {
			
			//if (selectedCheckbox.get(s.getSystemUserID())) {
				SystemUser systemUser = new SystemUser();
				systemUser.setSystemUserID(s.getSystemUserID());
				systemUserChangeStateService.changeSystemUserStateToActive(systemUser);
			//}
		}

		systemUserList = systemUserService.getSystemUserByState("disable");
		this.systemUserDataModel = new SystemUserDataModel(
				this.systemUserList);

		return "ChangeStateUser";
	}
	//search button
			public String search() {
				if("idCardNumber".equals(selectSearchType)) {
					systemUserDataModel = new SystemUserDataModel(systemUserService.getSystemUserByIDCardNumber(searchValue));			
				} else if("firstName".equals(selectSearchType)) {
					systemUserDataModel = new SystemUserDataModel(systemUserService.getSystemUserByFirstName(searchValue));
				} else if("email".equals(selectSearchType)) {
					systemUserDataModel = new SystemUserDataModel(systemUserService.getSystemUserByEmail(searchValue));
				} else if("systemUserID".equals(selectSearchType)) {
					systemUserDataModel = new SystemUserDataModel(systemUserService.getSystemUserByID(Long.parseLong(searchValue)));
				}
		 		return "ChangeStateUser";
			}
			
			public void changeSelectButton(ValueChangeEvent e) {
				selectedUserState = e.getNewValue().toString();
				if ("active".equals(selectedUserState)) {
					changeButtonValue = "Active";
					systemUserList = systemUserService.getSystemUserByState(e
							.getNewValue().toString());
					this.systemUserDataModel = new SystemUserDataModel(
							this.systemUserList);
				} else if ("disable".equals(selectedUserState)) {
					changeButtonValue = "Disable";
					systemUserList = systemUserService.getSystemUserByState(e
							.getNewValue().toString());
					this.systemUserDataModel = new SystemUserDataModel(
							this.systemUserList);
				}
			}
			
}
